//
//  RestView.swift
//  Motion
//
//  Created by 悦月越悦 on 2021/4/8.
//

import SwiftUI

struct RestView: View {
    @Binding var isPresent: Bool
    @Binding var preparePresent:Bool
    @StateObject var timer:CustomerTimer = CustomerTimer(interval: 1, count: 10, type: .Subtract)
    
    var duration:Int
    var action :()->Void
    
    init(isPresent:Binding<Bool>,preparePresent:Binding<Bool>,count:Int,completion: @escaping ()->Void) {
        self._isPresent = isPresent
        self._preparePresent = preparePresent
        duration = count
        action = completion
    }
    
    var body: some View {
        Text("Rest\(timer.count)")
        .onChange(of: timer.count, perform: { value in
            print(value)
            if(value == 0){
                timer.cancel()
                isPresent = false
                preparePresent = true
                action()
            }
        })
        .onAppear(){
            print("onAppear RestView")
            timer.count = duration
            timer.startTimer()
        }
    }
}

